cc.Class({
    extends: cc.Component,

    properties: {
        
    },

    // use this for initialization
    onLoad: function () {
        // 注册点击事件监听
        this.node.on(cc.Node.EventType.TOUCH_END, this.onClicked, this);

        // 记录初始点的坐标
        var avatar = cc.find('hero', this.node);
        var originPos = avatar.getPosition();
        originPos = avatar.convertToWorldSpace(originPos);
        this.originPos = originPos;
    },

    onClicked: function(event){
        // 获取触摸点的坐标
        var touchPos = cc.v2(event.getLocation());

        var pos = cc.find('pos', this.node);
        pos.getComponent('cc.Label').string = '({0}, {1})'.format(touchPos.x, touchPos.y);

        if (touchPos.equals(this.originPos)){
            // do nothing, WHY?
        }
        else {
            // 计算角度
            var tempVec = cc.v2(touchPos.x - this.originPos.x, touchPos.y - this.originPos.y);
            var directVec = touchPos.sub(this.originPos);
            var angle = cc.v2(1, 0).signAngle(directVec) * 180 / Math.PI;
            this.updateAvatarRotation(angle);
        }
    },

    // 修改 hero 的方向
    updateAvatarRotation: function(angle){
        cc.find('hero/spr_avatar', this.node).rotation = angle;
    }
});
